package cn.forkway;

import cn.forkway.dao.StudentDao;
import cn.forkway.domain.Student;
import cn.forkway.queryParam.stuQueryParam;
import cn.forkway.resultMap.stuMap;
import cn.forkway.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.List;

/**
 * @program: java-learn-mybatis
 * @className: testMapper.java
 * @author: River Ray
 * @description: 动态代理的方式去调用Dao
 * @createTime 2021年10月10日 14:56:00
 */
public class testMapper {

   private  static StudentDao dao;

   @BeforeClass
   public static void init(){
      SqlSession sqlSession = MybatisUtils.getSession();
      dao = sqlSession.getMapper(StudentDao.class);
   }
   @Test
   public void select(){
      List<Student> studentList = dao.selectStudent();
      studentList.forEach(System.out::println);
   }

   @Test
   public void selectById(){
      List<Student> studentList = dao.selectStudentById(1001);
      studentList.forEach(System.out::println);
   }
   @Test
   public void selectByNamAndAge(){
      List<Student> studentList =dao.selectStudentByNameAge(22,"river");
      studentList.forEach(System.out::println);
   }

   @Test
   public void  selectStudentByQueryParam(){
      stuQueryParam param = new stuQueryParam();
      param.setParamAge(89);
      param.setParamName("river");
      List<Student> studentList = dao.selectStudentByQueryParam(param);
      studentList.forEach(System.out::println);
   }

   @Test
   public void  selectStudentByQueryParamResultMap(){
      stuQueryParam param = new stuQueryParam();
      param.setParamAge(89);
      param.setParamName("river");
      List<stuMap> studentList = dao.selectStudentByQueryParamResultMap(param);
      studentList.forEach(System.out::println);
   }

//   测试自动提交事务
   public void Insert(){
      SqlSession sqlSession = MybatisUtils.getSessions(true);
      StudentDao dao = sqlSession.getMapper(StudentDao.class);
      Student student = new Student();
      student.setName("sed");
      student.setEmail("12312@11.com");
      student.setAge(23);
      int nums = dao.insertStudent(student);
      sqlSession.commit();
      System.out.println(nums);
   }

}
